Option Explicit On
Option Strict On

Imports System.Collections
Imports System.Reflection
Imports System.Collections.Generic
Imports NUnit.Framework
Imports nstl



Namespace nstl.test



    Public MustInherit Class IDictionaryTest

        Protected MustOverride Function CreateDictionary() As IDictionary(Of Integer, String)

        <SetUp()> _
        Public Overridable Sub SetUp()
            dict = CreateDictionary()

            keys = New Vector(Of Integer)()
            vals = New Vector(Of String)()
            For i As Integer = 0 To 9
                keys.PushBack(i)
            Next

            Algorithm.RandomShuffle(keys.Begin, keys.End)

            For Each i As Integer In keys
                dict.Add(i, i.ToString())
                vals.PushBack(i.ToString())
            Next
        End Sub

        <TearDown()> _
        Public Overridable Sub TearDown()
            dict = Nothing
            keys = Nothing
            vals = Nothing
        End Sub
        Private dict As IDictionary(Of Integer, String)
        Private keys As Vector(Of Integer)
        Private vals As Vector(Of String)

        <Test()> _
        Public Sub TestAdd()
            dict.Add(42, "Lulli")
            Assert.AreEqual(dict(42), "Lulli")
        End Sub

        <Test()> _
        Public Sub TestContainsKey()
            Assert.IsTrue(dict.ContainsKey(5))
            Assert.IsFalse(dict.ContainsKey(42))
        End Sub

        <Test()> _
        Public Sub TestRemove()
            Assert.IsTrue(dict.ContainsKey(5))
            dict.Remove(5)
            Assert.IsFalse(dict.ContainsKey(5))
        End Sub

        <Test()> _
        Public Sub TestTryGetValue()

            Dim val As String = ""
            Assert.IsTrue(dict.TryGetValue(5, val))
            Assert.AreEqual("5", val)

            Assert.IsFalse(dict.TryGetValue(42, val))
            Assert.IsNull(val)
        End Sub

        <Test()> _
        Public Sub TestIndexer()
            Algorithm.RandomShuffle(keys.Begin, keys.End)

            For Each i As Integer In keys
                Assert.AreEqual(i.ToString(), dict(i))
            Next
        End Sub
        <Test()> _
        Public Sub TestKeysProp()

            Dim dictKeys As New Vector(Of Integer)(dict.Keys)
            Algorithm.Sort(dictKeys.Begin, dictKeys.End)
            Algorithm.Sort(keys.Begin, keys.End)
            Assert.IsTrue(TestUtil.equal(dictKeys, keys))
        End Sub
        <Test()> _
        Public Sub TestValueProp()
            Dim dictVals As New Vector(Of String)(dict.Values)
            Algorithm.Sort(dictVals.Begin, dictVals.End)
            Algorithm.Sort(vals.Begin, vals.End)
            Assert.IsTrue(TestUtil.equal(dictVals, vals))
        End Sub

    End Class


End Namespace


